package org.aeon.heroicsagatextedition.round.product;

import org.aeon.heroicsagatextedition.Input.InputFactory;
import org.aeon.heroicsagatextedition.commands.AttackCommand;
import org.aeon.heroicsagatextedition.commands.AttackInvoker;
import org.aeon.heroicsagatextedition.commands.Command;
import org.aeon.heroicsagatextedition.constants.CommonlyConstants;
import org.aeon.heroicsagatextedition.game.Backup.GameBackup;
import org.aeon.heroicsagatextedition.game.GameSettlement;
import org.aeon.heroicsagatextedition.output.OutputFactory;
import org.aeon.heroicsagatextedition.role.product.RoleUnit;

import java.util.Deque;
import java.util.Iterator;

import static org.aeon.heroicsagatextedition.constants.Color.BACKGROUND_RED;
import static org.aeon.heroicsagatextedition.constants.Color.BRIGHT_WHITE;
import static org.aeon.heroicsagatextedition.utils.FontColors.coloring;

/**
 * @projectName: HeroicSagaTextEdition
 * @package: org.aeon.heroicsagatextedition.round
 * @className: HeroRound
 * @author: MissingAeon
 * @date: 2023/12/22 15:13
 * @description:
 */
public class HeroRound extends Round {
    public HeroRound(RoleUnit role) {
        super(role);
    }

    @Override
    public void roundResponse() {
        OutputFactory.create().print(coloring(role.getName(), BRIGHT_WHITE) + "，开始回合行动：");
        action();
        InputFactory.create().pauseBeforeInput();
        super.roundResponse();
    }

    private void action() {
        while (true) {
            Deque<RoleUnit> campPlayers = super.displayCampPlayers(CommonlyConstants.EVIL);
            OutputFactory.create().print(false, "--选择您的行动(‘^[目标序号]’攻击，‘*[目标序号]’查看，‘#’使用道具，‘!’保存退出):");
            String actionCommand = InputFactory.create().scanString();
            if ("!".equals(actionCommand)) {
                if (saveExit()) return;
            } else if ("#".equals(actionCommand)) {
                if (usingPills()) return;
            } else {
                int number = 0;
                try {
                    number = Integer.parseInt(actionCommand.substring(1));
                } catch (NumberFormatException e) {
                    OutputFactory.create().print(coloring("输入的行动非法！", BACKGROUND_RED));
                }
                if (number != 0) {
                    RoleUnit target = selectedTarget(campPlayers, number);
                    if (target != null) {
                        if (actionCommand.startsWith("^")) {
                            attackTarget(target);
                            return;
                        } else if (actionCommand.startsWith("*")) {
                            OutputFactory.create().print(false, target.toString());
                        }
                    }
                }
            }
        }
    }

    private void attackTarget(RoleUnit target) {
        OutputFactory.create().print("--攻击目标：" + target.getName());
        Command command = AttackCommand.newGoals(target, role.getAttack());
        AttackInvoker attackInvoker = new AttackInvoker();
        attackInvoker.setCommand(command);
        attackInvoker.attack();
    }

    private RoleUnit selectedTarget(Deque<RoleUnit> campPlayers, int targetNumber) {
        if (targetNumber > 0 && targetNumber <= campPlayers.size()) {
            Iterator<RoleUnit> iterator = campPlayers.iterator();
            for (int i = 0; i < targetNumber - 1; i++) {
                iterator.next();
            }
            return iterator.next();
        }
        return null;
    }

    private boolean usingPills() {
        if (role.getLifeSavingPillCount() > 0 && !role.isFullOfHp()) {
            OutputFactory.create().print("确认否使用救命药丸并回合结束(y/n)?");
            String choice = InputFactory.create().scanString();
            if ("Y".equalsIgnoreCase(choice)) {
                return role.usingPills();
            }
        }
        OutputFactory.create().print("血量达到上限！");
        return false;
    }

    private boolean saveExit() {
        OutputFactory.create().print("是否确认保存并退出(y/n)?");
        String choice = InputFactory.create().scanString();
        if ("Y".equalsIgnoreCase(choice)) {
            GameBackup.createMemento(roles, getRecordCount());
            new GameSettlement();
            return true;
        }
        return false;
    }
}
